using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Kitware.AvaloniaControls;
using Kitware.VTK;
using System;

namespace HelloVTKAvalonia
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void InitializeComponent()
    {
      AvaloniaXamlLoader.Load(this);

      RenderWindowControl ctrl = this.FindControl<RenderWindowControl>("VTKControl");
      if (ctrl != null)
      {
        ctrl.AttachedToVisualTree += InitVTKScene;
      }
    }

    public void InitVTKScene(object? sender, EventArgs args)
    {
      RenderWindowControl? mainView = sender as RenderWindowControl;

      vtkRenderWindow renderWindow = mainView.RenderWindow;

      vtkRenderer renderer = vtkRenderer.New();
      renderer.SetBackgroundAlpha(1.0);

      renderWindow.AddRenderer(renderer);

      vtkInteractorStyleTrackballCamera interactorStyle = vtkInteractorStyleTrackballCamera.New();
      renderWindow.GetInteractor().SetInteractorStyle(interactorStyle);

      vtkSphereSource src = vtkSphereSource.New();
      vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
      mapper.SetInputConnection(src.GetOutputPort());
      vtkActor actor = vtkActor.New();
      actor.SetMapper(mapper);
      renderer.AddActor(actor);
    }
  }
}
